/**
 * jims
 */
package com.jims.master.modules.med.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.DateUtils;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.finance.entity.OutpAcctMaster;
import com.jims.master.modules.finance.entity.OutpPaymentsMone;
import com.jims.master.modules.finance.service.OutpAcctMasterService;
import com.jims.master.modules.med.entity.MedicalCardChangeLog;
import com.jims.master.modules.med.entity.MedicalCardConsumeDetail;
import com.jims.master.modules.med.entity.MedicalCardMaster;
import com.jims.master.modules.med.entity.MedicalCardRechangeRec;
import com.jims.master.modules.med.service.MedicalCardChangeLogService;
import com.jims.master.modules.med.service.MedicalCardConsumeDetailService;
import com.jims.master.modules.med.service.MedicalCardMasterService;
import com.jims.master.modules.med.service.MedicalCardRechangeRecService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 一卡通办理Controller
 * @author 陈晓阳
 * @version 2017-05-22
 */
@Controller
@RequestMapping(value = "${adminPath}/med/medicalCardMaster")
public class MedicalCardMasterController extends BaseController {
    @Autowired
    private OutpAcctMasterService outpAcctMasterService;
	@Autowired
	private MedicalCardMasterService medicalCardMasterService;
    @Autowired
    private MedicalCardRechangeRecService medicalCardRechangeRecService;
    @Autowired
    private MedicalCardChangeLogService medicalCardChangeLogService;
    @Autowired
    private MedicalCardConsumeDetailService medicalCardConsumeDetailService;
	
	@ModelAttribute
	public MedicalCardMaster get(@RequestParam(required=false) String id) {
		MedicalCardMaster entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = medicalCardMasterService.get(id);
		}
		if (entity == null){
			entity = new MedicalCardMaster();
		}
		return entity;
	}

    /**
     * 列表页面
     * @param medicalCardMaster
     * @param request
     * @param response
     * @param model
     * @return
     */
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(MedicalCardMaster medicalCardMaster, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<MedicalCardMaster> page = medicalCardMasterService.findPage(new Page<MedicalCardMaster>(request, response), medicalCardMaster); 
		model.addAttribute("page", page);
		return "modules/med/medicalCardMasterIndex";
	}

    /**
     * 办理页面
     * @param medicalCardMaster
     * @param model
     * @return
     */
	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(MedicalCardMaster medicalCardMaster, Model model) {
		model.addAttribute("medicalCardMaster", medicalCardMaster);
		return "modules/med/medicalCardMasterForm";
	}


    /**
     * 一卡通保存操作
     * @param medicalCardMaster
     * @param model
     * @return
     */
	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(MedicalCardMaster medicalCardMaster, Model model) {
        String flag=medicalCardMaster.getFlag();
	    String message="";
        switch (Integer.parseInt(flag)){
            case 1:
                message="新建";
                break;
            case 2:
                message="修改";
                break;
            case 3:
                message="充值";
                break;
            case 4:
                message="补办";
                break;
            case 5:
                message="退卡";
                break;
            default:message="操作";
                break;
        }
	    StringData data=new StringData();
        Map<String,Object> map=new HashMap<String,Object>();
        map=medicalCardMasterService.save(medicalCardMaster,map);
        Integer i=Integer.parseInt(map.get("rows")+"");
        if(i>0){
            data.setCode(map.get("balance")+"");
            data.setData(message+"成功");
        }else{
            data.setCode("error");
            data.setData(message+"失败");
        }
        return data;
	}



	@RequiresPermissions("user")
	@RequestMapping(value = "update")
	@ResponseBody
	public StringData update(MedicalCardMaster medicalCardMaster) {
	    StringData data=new StringData();
        String info="";
        if("1".equals(medicalCardMaster.getCardStatus())){
             //挂失
            info="挂失";
        }else{
            info="启用";
        }
		int i=medicalCardMasterService.update(medicalCardMaster);
        if(i>0){
            data.setCode("success");
            data.setData(info+"成功");
        }else{
            data.setCode("error");
            data.setData(info+"失败");
        }
		return data;
	}

    /**
     * 根据一卡通卡号查询相关信息
     * @param medicalCardMaster
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "findByCardNo")
    @ResponseBody
    public MedicalCardMaster findByCardNo(MedicalCardMaster medicalCardMaster, Model model) {
        return medicalCardMasterService.findByCardNo(medicalCardMaster);
    }

    /**
     * 根据一卡通卡号查询相关信息
     * @param medicalCardMaster
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "findByIdCard")
    @ResponseBody
    public MedicalCardMaster findByIdCard(MedicalCardMaster medicalCardMaster, Model model) {
        return medicalCardMasterService.findByIdCard(medicalCardMaster);
    }

    /**
     * 执行科室刷卡数据查询
     * @param medicalCardMaster
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"swingCardData", ""})
    public String swingCardData(MedicalCardMaster medicalCardMaster, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<MedicalCardMaster> page = medicalCardMasterService.findPage(new Page<MedicalCardMaster>(request, response), medicalCardMaster);
        model.addAttribute("page", page);
        return "modules/med/medicalCardMasterIndex";
    }
     /**
     * 一卡通 结账页面
     * @param startTime
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "cardAcctForm")
    public String swingCard(String startTime,Model model) {
        startTime=medicalCardRechangeRecService.getStartAcct();
        model.addAttribute("startTime",startTime);
        model.addAttribute("endTime",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date()));
        return "modules/med/cardAcctForm";
    }
      /**
     * 一卡通结账确认
     * @param startTime
     * @param endTime
     * @return
     * @author chenxy
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "confirmCardAcct")
    @ResponseBody
    public StringData confirmCardAcct(String startTime,String endTime){
        StringData data=new StringData();
        int i = medicalCardRechangeRecService.confirmAcct(startTime,endTime);
        if(i>0){
            data.setCode("success");
        }else{
            data.setCode("error");
        }
        return data;
    }
    /**
     *查询支付方式以及详细信息
     * @param model
     * @param startTime
     * @param endTime
     * @param medicalCardRechangeRec
     * @return
     * @author chenxy
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "settleDetails")
    public String settleDetails(Model model , String startTime,String endTime,MedicalCardRechangeRec medicalCardRechangeRec){
        // 充值统计
        medicalCardRechangeRec=this.medicalCardRechangeRecService.countRechange(startTime,endTime);
        model.addAttribute("medicalCardRechangeRec",medicalCardRechangeRec);
        //退卡统计
        MedicalCardChangeLog medicalCardChangeLog=medicalCardChangeLogService.countReturnCard(startTime, endTime);
        model.addAttribute("medicalCardChangeLog",medicalCardChangeLog);
        //显示明细
        medicalCardRechangeRec.setStartTime(startTime);
        medicalCardRechangeRec.setEndTime(endTime);
        medicalCardRechangeRec.setAcctNo("111");
        List<MedicalCardRechangeRec> list=medicalCardRechangeRecService.findList(medicalCardRechangeRec);
        model.addAttribute("list",list);
        return "modules/med/paymentsSettleBelow";
    }

    /**
     * 一卡通消费统计
     * @param model
     * @param endTime
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "consumeAcct")
    public String cardConsume(Model model,String endTime,OutpAcctMaster outpAcctMaster){
        if(endTime==null || endTime.equals("")){
            endTime= DateUtils.getDate("yyyy-MM-dd HH:mm:ss");
        }
        outpAcctMaster.setAcctType("2");
        outpAcctMaster.setEndTime(endTime);
        outpAcctMaster.setCardFlag("0");
        OutpAcctMaster master=outpAcctMasterService.mergeAcctGetInfo(outpAcctMaster);
        master.setCardFlag("0");
        List<OutpPaymentsMone> paymentsMones = outpAcctMasterService.mergeAcctGetPayInfo(outpAcctMaster);
        model.addAttribute("endTime",endTime);
        model.addAttribute("outpAcctMaster",master);
        model.addAttribute("paymentsMones",paymentsMones);
        return "/modules/finance/mergeAcctIndex";
    }

     /**
     * 一卡通 退卡统计
     * @param medicalCardRechangeRec
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "returnCardAcct")
    public String returnCardAcct(MedicalCardRechangeRec medicalCardRechangeRec,Model model,HttpServletRequest request, HttpServletResponse response) {
        medicalCardRechangeRec.setRechangeFlag("1");
        Page<MedicalCardRechangeRec> page = medicalCardRechangeRecService.findPage(new Page<MedicalCardRechangeRec>(request, response), medicalCardRechangeRec);
        model.addAttribute("page", page);
        model.addAttribute("medicalCardRechangeRec",medicalCardRechangeRec);
        return "modules/med/returnCardAcct";
    }


    /**
     * 一卡通 消费查询
     * @param medicalCardConsumeDetail
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "syAcct")
    public String sysAcct(MedicalCardConsumeDetail medicalCardConsumeDetail,Model model,HttpServletRequest request, HttpServletResponse response) {
        String cardNo=medicalCardConsumeDetail.getCardNo();
//        if(cardNo!=null&&!"".equals(cardNo)){
            Page<MedicalCardConsumeDetail> page = medicalCardConsumeDetailService.findPage(new Page<MedicalCardConsumeDetail>(request, response), medicalCardConsumeDetail);
            model.addAttribute("page", page);
            model.addAttribute("medicalCardConsumeDetail",medicalCardConsumeDetail);
//        }
        return "modules/med/sysAcct";
    }


    /**
     * 充值打印
     * @param medicalCardMaster
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "printDeposit")
    @ResponseBody
    public StringData printDeposit(MedicalCardMaster medicalCardMaster){
        StringData stringData = new StringData();
        String printPath =medicalCardMasterService.printDeposit(medicalCardMaster);
        stringData.setCode("success");
        stringData.setData(printPath);
        return stringData;
    }

    /**
     * 发药打印
     * @param medicalCardMaster
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "printItem")
    @ResponseBody
    public StringData printItem(MedicalCardMaster medicalCardMaster){
        StringData stringData = new StringData();
        String printPath =medicalCardMasterService.printItem(medicalCardMaster);
        stringData.setCode("success");
        stringData.setData(printPath);
        return stringData;
    }
}